package com.ruoyi.hospital.patient.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.hospitalized.mapper.THospitalizedMapper;
import com.ruoyi.hospital.patient.domain.TPatient;
import com.ruoyi.hospital.patient.mapper.TPatientMapper;
import com.ruoyi.hospital.patient.service.ITPatientService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.mapper.TRegisteredMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 患者管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Service
public class TPatientServiceImpl implements ITPatientService {
    @Autowired
    private TPatientMapper tPatientMapper;

    @Autowired
    private TRegisteredMapper tRegisteredMapper;

    @Autowired
    private THospitalizedMapper tHospitalizedMapper;

    /**
     * 查询患者管理
     *
     * @param id 患者管理主键
     * @return 患者管理
     */
    @Override
    public TPatient selectTPatientById(Long id) {
        return tPatientMapper.selectTPatientById(id);
    }

    /**
     * 查询患者管理列表
     *
     * @param tPatient 患者管理
     * @return 患者管理
     */
    @Override
    public List<TPatient> selectTPatientList(TPatient tPatient) {
        return tPatientMapper.selectTPatientList(tPatient);
    }

    /**
     * 新增患者管理
     *
     * @param tPatient 患者管理
     * @return 结果
     */
    @Override
    public int insertTPatient(TPatient tPatient) {
        List<TPatient> tPatientList = tPatientMapper.selectTPatientByName(tPatient.getName());
        if (!tPatientList.isEmpty()) {
            throw new ServiceException("患者姓名不允许重复");
        }
        tPatient.setCreatedTime(new Date());
        tPatient.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tPatientMapper.insertTPatient(tPatient);
    }

    /**
     * 修改患者管理
     *
     * @param tPatient 患者管理
     * @return 结果
     */
    @Override
    public int updateTPatient(TPatient tPatient) {
        return tPatientMapper.updateTPatient(tPatient);
    }

    /**
     * 批量删除患者管理
     *
     * @param ids 需要删除的患者管理主键
     * @return 结果
     */
    @Override
    public int deleteTPatientByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            checkDelete(id);
        }
        return tPatientMapper.deleteTPatientByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除患者管理信息
     *
     * @param id 患者管理主键
     * @return 结果
     */
    @Override
    public int deleteTPatientById(Long id) {
        return tPatientMapper.deleteTPatientById(id);
    }

    private void checkDelete(String patientId) {
        TRegistered tRegistered = new TRegistered();
        tRegistered.setPatientId(Long.parseLong(patientId));
        List<TRegistered> tRegisteredList = tRegisteredMapper.selectTRegisteredList(tRegistered);
        if (!tRegisteredList.isEmpty()) {
            throw new ServiceException("已有门诊引用，不能删除患者");
        }
        THospitalized tHospitalized = new THospitalized();
        tHospitalized.setPatientId(patientId);
        List<THospitalized> tHospitalizedList = tHospitalizedMapper.selectTHospitalizedList(tHospitalized);
        if (!tHospitalizedList.isEmpty()) {
            throw new ServiceException("已有住院管理引用，不能删除患者");
        }
    }
}
